// pages/Protocol/Reward/Reward.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryRewardRankings , rewardTopUp } from '../../../api/protocol';
import { queryUserWechatOpenId } from '../../../api/user';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    amount: '',
    amountList: [
      { id: 1, name: '1元'},{ id: 2, name: '2元'},{ id: 5, name: '5元'},{ id: 8, name: '8元'},{ id: 10, name: '10元'},{ id: 20, name: '20元'},
    ],
    isCustom: '0', // 是否自定义
    otherAmount: '', // 其他金额
    rankings: [], // 打赏排行
    openid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._queryUserWechatUnionId();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryRewardRankings();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId')
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 选择打赏金额
  selAmount(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    this.setData({
      amount: item.id,
      isCustom: '0',
      otherAmount: '',
    })
  },
  // 其他金额输入
  onOtherAmountChange(event){
    this.setData({
      otherAmount: event.detail,
      isCustom: '1',
    })
  },
  // 打赏记录
  goBill(){
    wx.navigateTo({
      url: '/pages/Protocol/RewardBill/RewardBill',
    })
  },
  // 查询打赏排行
  queryRewardRankings(){
    let _this = this;
    queryRewardRankings({page: 0,})
    .then(res => {
      if(res.success){
        // _this.rankings = res.data.content;
        _this.setData({
          rankings: res.data.content,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 打赏操作
  submit(){
    let _this = this;
    const { isCustom , amount , otherAmount , openid } = _this.data;
    rewardTopUp({userId: wx.getStorageSync('userId'),number: isCustom == '0' ? amount : otherAmount,openId: openid})
    .then(res => {
      if(res.success){
        var order = JSON.parse(res.data.data)
        // TODO微信支付
        wx.requestPayment({
          timeStamp: order.timeStamp.toString(),
          nonceStr: order.nonceStr,
          package: order.package,
          signType: order.signType,
          paySign: order.paySign,
          success: () => {
            wx.showToast({
              title: '打赏成功',
              icon: 'success'
            })
            _this.queryRewardRankings();
          },
          fail: (err) => {
            console.log(err,'err')
            wx.showToast({
              title: '取消打赏',
              icon: 'none',
            })
          }
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取用户的Openid
  _queryUserWechatUnionId(){
    let _this = this;
    wx.login({
      success: (logRes) => {
        queryUserWechatOpenId({code: logRes.code})
        .then(res => {
          if(res.success){
            _this.setData({
              openid: res.data.openid
            })
          }else{
            wx.showToast({
              title: res.errMessage,
              icon: 'none',
            })
          }
        })
      }
    })
  },
})